#include "sal.h"

sal_err_t
ctc_sal_init(void)
{
    sal_err_t ret;

    ret = sal_timer_init();

#ifdef SAL_DEBUG_MUTEX
    ret += sal_mutex_init();
#endif
    return ret;
}

void
ctc_sal_deinit(void)
{
    sal_timer_fini();
}

#if defined(_SAL_LINUX_KM)

static int sort_of_seed;

int ctc_sal_get_rand()
{
    int rand_value = 0;
    get_random_bytes(&rand_value, sizeof(rand_value));

    return ((rand_value ^ sort_of_seed) & 0x7fffffff);
}

void
ctc_sal_srand(unsigned int seed)
{
    sort_of_seed = (seed << 19) ^ (seed << 13) ^ seed;
}

sal_time_t
ctc_sal_mktime(struct tm * __tp)
{
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
    return (sal_time_t)mktime64(__tp->tm_year, __tp->tm_mon, __tp->tm_mday, __tp->tm_hour, __tp->tm_min, __tp->tm_sec);
#else
    return (sal_time_t)mktime(__tp->tm_year, __tp->tm_mon, __tp->tm_mday, __tp->tm_hour, __tp->tm_min, __tp->tm_sec);
#endif
}


#include <asm/div64.h>

unsigned long long __udivdi3 (unsigned long long a, unsigned long long b) {
    unsigned long long x = a, y = b, result;
    unsigned long mod;

    mod = do_div(x, y);
    result = x;
    return result;
}


long long __divdi3 (long long a, long long b) {
    unsigned long long x = a, y = b, result;
    unsigned long mod;

    mod = do_div(x, y);
    result = x;
    return result;
}

long long __umoddi3(long long a, long long b) {
    unsigned long long x = a, y = b;
    unsigned long mod;

    mod = do_div(x, y);
    return mod;
}
/*
double __subdf3(double a, double b)
{
	return a-b;
}*/
#else
int ctc_sal_get_rand()
{
    int fd = 0;
    int rand_value = 0;
    int size = 0;

    fd = sal_open("/dev/urandom",O_RDONLY);
    if(fd >= 0)
    {
        size =  sal_read(fd, &rand_value, sizeof(int));
        if(size < 0)
        {
            SAL_LOG_INFO("Notice!!!read urandom fail!");
        }
        sal_close(fd);
    }
    return (rand_value & 0x7fffffff);
}
#endif

